package com.flash.variety.controller;

import java.io.*;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.aliyun.oss.OSS;
import com.flash.variety.domain.Preferential;
import com.flash.variety.domain.PreferentialDTO;
import com.flash.variety.domain.PreferentialType;
import com.flash.variety.mapper.PreferentialMapper;
import com.flash.variety.mapper.PreferentialTypeMapper;
import com.flash.variety.service.impl.MyAsync;
import com.flash.variety.utils.PoiUtils;
import com.ruoyi.common.core.utils.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.flash.variety.domain.TbCuisine;
import com.flash.variety.service.ITbCuisineService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 菜品Controller
 * 
 * @author ruoyi
 * @date 2023-01-06
 */
@RestController
@RequestMapping("/cuisine")
@CrossOrigin
public class TbCuisineController extends BaseController
{
    @Autowired
    private ITbCuisineService tbCuisineService;

    /**
     * 查询菜品列表
     */
    /*@RequiresPermissions("variety:cuisine:list")*/
    @GetMapping("/list")
    public TableDataInfo list(TbCuisine tbCuisine)
    {
        startPage();
        Long userId = com.ruoyi.common.security.utils.SecurityUtils.getUserId();
        int intValue = userId.intValue();
        Integer a=(Integer)intValue;
        if(a.equals(1)){
            a=null;
        }
        if(a.equals(0)){
            a=null;
        }
        tbCuisine.setCommercialId(a);
        List<TbCuisine> list = tbCuisineService.selectTbCuisineList(tbCuisine);
        return getDataTable(list);
    }

    /**
     * 导出菜品列表
     */
    @RequiresPermissions("variety:cuisine:export")
    @Log(title = "菜品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbCuisine tbCuisine) throws IOException {
        Long userId = com.ruoyi.common.security.utils.SecurityUtils.getUserId();
        List<TbCuisine> list = tbCuisineService.selectTbCuisineList(tbCuisine);
/*        ExcelUtil<TbCuisine> util = new ExcelUtil<TbCuisine>(TbCuisine.class);
        util.exportExcel(response, list, "菜品数据");*/
         /*PoiUtils.poi(list);*/
    }


    /**
     * 获取菜品详细信息
     */
    @RequiresPermissions("variety:cuisine:query")
    @GetMapping(value = "/{cuisineId}")
    public AjaxResult getInfo(@PathVariable("cuisineId") Long cuisineId)
    {
        return AjaxResult.success(tbCuisineService.selectTbCuisineByCuisineId(cuisineId));
    }

    /**
     * 新增菜品
     */
    @RequiresPermissions("variety:cuisine:add")
    @Log(title = "菜品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbCuisine tbCuisine)
    {
        Date date = new Date();
        tbCuisine.setUpdatedate(date);
        return toAjax(tbCuisineService.insertTbCuisine(tbCuisine));
    }

    /**
     * 修改菜品
     */
    @RequiresPermissions("variety:cuisine:edit")
    @Log(title = "菜品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbCuisine tbCuisine)
    {
        Date date = new Date();
        tbCuisine.setUpdatedate(date);
        return toAjax(tbCuisineService.updateTbCuisine(tbCuisine));
    }

    /**
     * 删除菜品
     */
    @RequiresPermissions("variety:cuisine:remove")
    @Log(title = "菜品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{cuisineIds}")
    public AjaxResult remove(@PathVariable Long[] cuisineIds)
    {
        return toAjax(tbCuisineService.deleteTbCuisineByCuisineIds(cuisineIds));
    }

    @Autowired
    private MyAsync myAsync;

    @GetMapping("/dc")
    public void dc(TbCuisine tbCuisine) throws IOException {
        Long userId = com.ruoyi.common.security.utils.SecurityUtils.getUserId();
        List<TbCuisine> list = tbCuisineService.selectTbCuisineList(tbCuisine);
        myAsync.sms(list,userId);
    }

    @Value("${oss.bucket}")
    private String bucketName;

    @Value("${oss.hostName}")
    private String hostName;

    @Autowired
    private OSS client;

    @PostMapping("oss")
    public String ossUpload(@RequestParam("file") MultipartFile file){
        try {
            //OSS
            String fileName =  file.getOriginalFilename();
            client.putObject(bucketName,fileName,file.getInputStream());
          return   hostName+fileName;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "上传失败";
    }

    @Autowired
    RedisTemplate redisTemplate;

    @GetMapping("url")
    public String url(){
        Long userId = com.ruoyi.common.security.utils.SecurityUtils.getUserId();
        String url = (String) redisTemplate.opsForValue().get("url"+userId);
        if(!StringUtils.isEmpty(url)){
            return url;
        }else{
            return "请稍等,正在生成中";
        }
    }

   @PostMapping("dr")
   public void importData(@RequestBody MultipartFile file) throws IOException {
        //解决了一个问题
        InputStream inputStream = file.getInputStream();
       myAsync.dr(file);
       Date date = new Date();
       SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       System.err.println(format.format(date));
       System.out.println("正在导入");
    //调取实体对应的service层新增方
   }

    @PostMapping("dr1")
    public void importData1(@RequestBody MultipartFile[] files) throws IOException {
        //解决了一个问题
        System.out.println("b:"+Thread.currentThread().getName());
        for (int i = 0; i <files.length ; i++) {
            MultipartFile file = files[i];
            InputStream inputStream = file.getInputStream();
            myAsync.dr(file);
        }
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.err.println(format.format(date));
        System.out.println("正在导入");
        //调取实体对应的service层新增方
    }

    @Autowired
    PreferentialMapper preferentialMapper;

    @Autowired
    PreferentialTypeMapper preferentialTypeMapper;

    @GetMapping("type")
    public List<PreferentialDTO> getDto(){
        List<PreferentialType> type = preferentialTypeMapper.getType();
        List<PreferentialDTO> collect = type.stream().map(preferentialType -> {
            PreferentialDTO preferentialDTO = new PreferentialDTO();
            BeanUtils.copyProperties(preferentialType, preferentialDTO);
            List<Preferential> preferential = preferentialMapper.getPreferential(preferentialType.getType());
            preferentialDTO.setChildren(preferential);
            return preferentialDTO;
        }).collect(Collectors.toList());
        return collect;
    }
}
